package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.bean.LotteryImageBean;
import com.lottery.ruok.lottrey.bean.LotteryItem;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.Utils;

public class ChuantongcaiGridAdapter extends BaseAdapter {
    private ArrayList<LotteryItem> lists;
    private Context mContext;

    public interface ClickInterface {
        void click();
    }

    private ClickInterface clickInterface;
    private String mCode;

    public ChuantongcaiGridAdapter(ArrayList<LotteryItem> lists, Context context, ClickInterface c, String code) {
        this.lists = lists;
        mContext = context;
        clickInterface = c;
        mCode = code;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            if (Common.isK3(mCode)) {
                view = LayoutInflater.from(mContext).inflate(R.layout.chuantong_child_item2, viewGroup, false);
            } else {
                view = LayoutInflater.from(mContext).inflate(R.layout.chuantong_child_item, null);
            }
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        if (lists.get(i).isFen) {
            holder.odds.setText("封盘");
            holder.odds.setTextColor(mContext.getResources().getColor(R.color.color7));
        } else {
            if (StringUtils.isNotEmpty(lists.get(i).odd)){
                holder.odds.setText(lists.get(i).odd);
            }
            holder.odds.setTextColor(mContext.getResources().getColor(R.color.orange_one));
        }
        String name = lists.get(i).name;
        if (Utils.isNumeric(name)) {
            holder.name.setVisibility(View.GONE);
            holder.number.setVisibility(View.VISIBLE);
            holder.number.setText(name);
            Common.setBallBackground(holder.number, name, mCode);
        } else {
            holder.name.setText(name);
            holder.name.setVisibility(View.VISIBLE);
            holder.number.setVisibility(View.GONE);
        }
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lists.get(i).isChoose = !lists.get(i).isChoose;
                if (clickInterface != null) {
                    clickInterface.click();
                }
                notifyDataSetChanged();
            }
        });
        if (Common.isK3(mCode)) {
            if (lists.get(i).isFen) {
                //封盘了
                holder.content.setEnabled(false);

                holder.content.setBackgroundResource(R.drawable.child_item_bg_fen);

            } else {
                //未封盘
                holder.content.setEnabled(true);
                if (lists.get(i).isChoose) {
                    holder.content.setBackgroundResource(R.drawable.child_item_bg_pressed);
                } else {
                    holder.content.setBackgroundResource(R.drawable.child_item_bg_normal);
                }
            }

        } else {
            if (lists.get(i).isFen) {
                //封盘了
                holder.content.setEnabled(false);
                holder.content.setBackgroundResource(R.mipmap.cardfeng);
            } else {
                //未封盘
                holder.content.setEnabled(true);
                if (lists.get(i).isChoose) {
                    holder.content.setBackgroundResource(R.mipmap.card);
                } else {
                    holder.content.setBackgroundResource(R.mipmap.cardno);
                }
            }
        }
        List<LotteryImageBean> drawables = Common.getDrawableArray(mCode, name);


        if (drawables != null && !drawables.isEmpty()) {
            holder.name.setVisibility(View.GONE);
            holder.images_layout.setVisibility(View.VISIBLE);
            holder.images_layout.removeAllViews();
            for (int j = 0; j < drawables.size(); j++) {
                LotteryImageBean lotteryImage = drawables.get(j);
                ImageView dice = new ImageView(mContext);
                dice.setImageResource(lotteryImage.getRes());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                ;
                if (lotteryImage.getWidth() > 0) {
                    layoutParams.width = Utils.dip2px(mContext, lotteryImage.getWidth());
                }
                if (lotteryImage.getHeight() > 0) {
                    layoutParams.height = Utils.dip2px(mContext, lotteryImage.getHeight());
                }
                layoutParams.rightMargin = Utils.dip2px(mContext, 8);
                if (lotteryImage.getMarginTop() > 0) {
                    layoutParams.topMargin = Utils.dip2px(mContext, lotteryImage.getMarginTop());
                }
                dice.setLayoutParams(layoutParams);
                holder.images_layout.addView(dice);
            }
        } else {
            holder.name.setVisibility(View.VISIBLE);
            holder.images_layout.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.odds)
        TextView odds;
        @BindView(R.id.number)
        AreaCircle number;
        @BindView(R.id.content)
        LinearLayout content;
        @BindView(R.id.images_layout)
        LinearLayout images_layout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}